//2.利用第1题，构造3个以上图形，至少圆形、三角形、矩形各有一个，对上题的图形按照面积降序排列

// 这里使用了结构体和接口，目的是为了练习相关内容，实际上直接对结果进行排序即可
// 这里选择一个相对复杂的方法，通过构造结构体，，对 sort.Interface 接口进行覆写
package main

import (
	"fmt"
	"math"
	"sort"
)

type Shape interface {
	area() float64
}

type shapeArea struct {
	name string
	area float64
}

type triangleShape struct {
	length float64
	height float64
}

func (a *triangleShape) area() float64 {
	return a.length * a.height / 2
}

type circleShape struct {
	r float64
}

func (a *circleShape) area() float64 {
	return a.r * a.r * math.Pi
}

type rectangleShape struct {
	width  float64
	length float64
}

func (a *rectangleShape) area() float64 {
	return a.width * a.length
}

// type shapeAreaSlice []shapeArea

// func (a shapeAreaSlice) Len() int           { return len(a) }
// func (a shapeAreaSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
// func (a shapeAreaSlice) Less(i, j int) bool { return a[i].area > a[j].area }

func main() {
	a := triangleShape{3.4, 1.7}
	fmt.Printf("a.area(): %.3v\n", a.area())
	b := circleShape{5}
	fmt.Printf("b.area(): %.3v\n", b.area())
	c := rectangleShape{3, 4}
	fmt.Printf("c.area(): %.3v\n", c.area())

	d := make([]shapeArea, 0, 3)
	d = append(d, shapeArea{name: "triangle", area: a.area()})
	d = append(d, shapeArea{name: "circle", area: b.area()})
	d = append(d, shapeArea{name: "rectangle", area: c.area()})
	sort.Slice(d, func(i, j int) bool {
		return d[i].area > d[j].area
	})

	fmt.Println(d)

}
